﻿<?php
class User_Model_Categories extends Zend_Db_Table{
	protected $_name = 'categories';
	protected $_primary = 'category_id';
	/* Link tham khao : http://framework.zend.com/manual/en/zend.db.table.html */
	/* select demo */
	public function getAllCategories()
	{// co dung
		$client = new Zend_Rest_Client(HTTP_SERVICE);
		$return = $client->restGet("/categories/route/getAllCategories");
		$return = json_decode(json_encode((array) simplexml_load_string($return->getBody())),1);
		
		for ($i=0;$i <count($return);$i++)
				$data[$i] = $return['row_'.$i];
				
		return $data;
	}
	public function getCategoriesbyId($category_id)
	{// co dung
		$client = new Zend_Rest_Client(HTTP_SERVICE);
		$return = $client->restGet("/categories/route/getCategoriesbyId/category_id/".$category_id);
		$return = json_decode(json_encode((array) simplexml_load_string($return->getBody())),1);
		
		for ($i=0;$i <count($return);$i++)
				$data[$i] = $return['row_'.$i];
				
		return $data;
	}
	/*public function getProductCategories()
	{
		$select = $this->select();
		$select->from('categories');
		$select->setIntegrityCheck(false);
		$select->where('type = 1');
		$arr = $this->fetchAll($select);
		return $arr;
	}
	public function getparentCategories()
	{
		$select = $this->select();
		$select->from('categories');
		$select->setIntegrityCheck(false);
		$select->where('is_leaf = 0');
		$arr = $this->fetchAll($select);
		return $arr;
	}
	public function getParentProductCategories()
	{
		$select = $this->select();
		$select->from('categories');
		$select->setIntegrityCheck(false);
		$select->where('is_leaf = 0');
		$select->where('type = 1');
		$arr = $this->fetchAll($select);
		return $arr;
	}
	public function getLeafProductCategories()
	{
		$select = $this->select();
		$select->from('categories');
		$select->setIntegrityCheck(false);
		$select->where('is_leaf = 1');
		$select->where('type = 1');
		$arr = $this->fetchAll($select);
		return $arr;
	}
	public function getParentCategoriesbyType($type)
	{
		$select = $this->select();
		$select->from('categories');
		$select->setIntegrityCheck(false);
		$select->where('is_leaf = 0');
		$select->where('type = ?', $type);
		$arr = $this->fetchAll($select);
		return $arr;
	}
	
	public function getLeafCategories()
	{
		$select = $this->select();
		$select->from('categories');
		$select->setIntegrityCheck(false);
		//$select->where('parent_id = ?',$parentid);
		$select->where('is_leaf = 1');
		$arr = $this->fetchAll($select);
		return $arr;
	}*/
	public function getLeafCategoriesbyType($type)
	{// co dung
		$client = new Zend_Rest_Client(HTTP_SERVICE);
		$return = $client->restGet("/categories/route/getLeafCategoriesbyType/type/".$type);
		$return = json_decode(json_encode((array) simplexml_load_string($return->getBody())),1);

		for ($i=0;$i <count($return);$i++)
				$data[$i] = $return['row_'.$i];
				
		return $data;
	}
}
?>